package com.banciyuan.bcywebview.biz.main.newdiscover.index;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.TimelineCardType;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.ArticleDiscoverHolder;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.DailyDiscoverHolder;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.EventDiscoverHolder;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.GoodsDiscoverHolder;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.GroupPostDiscoverHolder;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.MainPicDiscoverHolder;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.NovelDiscoverHolder;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.GroupDiscoverHolder;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.TopicSingleHolder;
import com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder.VideoDiscoverHolder;
import com.banciyuan.bcywebview.utils.string.StringUtil;

import java.util.LinkedList;
import java.util.List;

import de.greenrobot.daoexample.model.Timeline;


/**
 * Created by hy on 2015/8/20.
 */
public class DiscoverAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int HEADER = 99;

    private RecyclerView.ViewHolder header;
    private Context context;
    private List<Timeline> list = new LinkedList<>();

    public DiscoverAdapter(Context context, List<Timeline> list, RecyclerView.ViewHolder header) {
        this.context = context;
        this.list = list;
        this.header = header;
    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HEADER:
                return header;
            case TimelineCardType.TOPIC_CARD:
                return new TopicSingleHolder(View.inflate(context, R.layout.discover_topicsingle_card, null));
            case TimelineCardType.COSILL_CARD:
                return new MainPicDiscoverHolder(View.inflate(context, R.layout.discover_item_main, null));
            case TimelineCardType.WRITER_CARD:
                return new NovelDiscoverHolder(View.inflate(context, R.layout.discover_item_write, null));
            case TimelineCardType.DAILY_CARD:
                return new DailyDiscoverHolder(View.inflate(context, R.layout.discover_item_daily, null));
            case TimelineCardType.VIDEO_CARD:
                return new VideoDiscoverHolder(View.inflate(context, R.layout.discover_item_video, null));
            case TimelineCardType.ARTICLE_CARD:
                return new ArticleDiscoverHolder(View.inflate(context, R.layout.discover_item_larticle, null));
            case TimelineCardType.GOODS_CARD:
                return new GoodsDiscoverHolder(View.inflate(context, R.layout.discover_item_goods, null));
            case TimelineCardType.EVENT_CARD:
                return new EventDiscoverHolder(View.inflate(context, R.layout.event_discover_item, null));
            case TimelineCardType.GROUP_POST_CARD:
                return new GroupPostDiscoverHolder(View.inflate(context, R.layout.discover_item_group_post, null));
            case TimelineCardType.GROUP_CARD:
                return new GroupDiscoverHolder(View.inflate(context, R.layout.discover_item_group, null));
            default:
                return new EmptyHolder(View.inflate(context, R.layout.smooth_blank, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TimelineCardType.TOPIC_CARD:
                ((TopicSingleHolder) holder).renderData(list.get(position - 1), context);
                break;
            case TimelineCardType.COSILL_CARD:
                ((MainPicDiscoverHolder) holder).renderData(list.get(position - 1), context);
                break;
            case TimelineCardType.WRITER_CARD:
                ((NovelDiscoverHolder) holder).renderData(list.get(position - 1), context);
                break;
            case TimelineCardType.DAILY_CARD:
                ((DailyDiscoverHolder) holder).renderData(list.get(position - 1), context);
                break;
            case TimelineCardType.GOODS_CARD:
                ((GoodsDiscoverHolder) holder).renderData(list.get(position - 1), context);
                break;
            case TimelineCardType.EVENT_CARD:
                ((EventDiscoverHolder) holder).renderData(list.get(position - 1), context);
                break;
            case TimelineCardType.GROUP_CARD:
                ((GroupDiscoverHolder) holder).renderData(list.get(position - 1), context);
                break;
            case TimelineCardType.GROUP_POST_CARD:
                ((GroupPostDiscoverHolder) holder).renderData(list.get(position - 1), context);
                break;
            case TimelineCardType.VIDEO_CARD:
                ((VideoDiscoverHolder) holder).renderData(list.get(position - 1), context);
                break;
            case TimelineCardType.ARTICLE_CARD:
                ((ArticleDiscoverHolder) holder).renderData(list.get(position - 1), context);
                break;
            case HEADER:
                break;
            default:
                ((EmptyHolder) holder).renderData();
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADER;
        } else {
            Timeline mItem = list.get(position - 1);
            switch (TimelineFactory.whichType(mItem)) {
                case TimelineFactory.TIMELINE_TYPE_ILLUST:
                case TimelineFactory.TIMELINE_TYPE_COSER:
                default:
                    return TimelineCardType.COSILL_CARD;
                case TimelineFactory.TIMELINE_TYPE_DAILY:
                    if (StringUtil.notNullEqual(mItem.getType(), "larticle")) {
                        return TimelineCardType.ARTICLE_CARD;
                    } else if (StringUtil.notNullEqual(mItem.getType(), "svideo")) {
                        return TimelineCardType.VIDEO_CARD;
                    } else {
                        return TimelineCardType.DAILY_CARD;
                    }
                case TimelineFactory.TIMELINE_TYPE_WRITER:
                    return TimelineCardType.WRITER_CARD;
                case TimelineFactory.TIMELINE_TYPE_GROUP_TEAM:
                    return TimelineCardType.GROUP_CARD;
                case TimelineFactory.TIMELINE_TYPE_GROUP:
                    return TimelineCardType.GROUP_POST_CARD;
                case TimelineFactory.TIMELINE_TYPE_GOODS:
                    return TimelineCardType.GOODS_CARD;
                case TimelineFactory.TIMELINE_TYPE_BANNER:
                    return TimelineCardType.EVENT_CARD;
                case TimelineFactory.TIMELINE_TYPE_TOPIC:
                    return TimelineCardType.TOPIC_CARD;
            }
        }
    }

    private class EmptyHolder extends RecyclerView.ViewHolder {

        private View view;

        public EmptyHolder(View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.blank);
        }

        public void renderData() {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
            view.setLayoutParams(layoutParams);
        }
    }

}
